﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SRF.Common;
using SRF.Admin;
using SRF.Admin.Models;

namespace SRF.Web.Areas.Admin.Controllers
{
    [Authorize]
    [SRF.Common.Logging.Log]
    public class RoleController : Controller
    {
        static SRF.Common.Logging.ILog logger = SRF.Common.Logging.LogManager.GetLogger("RoleController");

        RoleService roleService = ObjectHelper.GetObject<RoleService>();

        //
        // GET: /Admin/Role/  
        [SRF.UI.ViewTitle("角色列表")]
        public ActionResult List()
        {
            SRF.UI.DataSource datasource = new UI.DataSource("admin.role.list", "admin.role.count");
            datasource.PerPageSize = 10;

            return View(datasource);
        }

        /// <summary>
        /// 编辑角色
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns> 
        public ActionResult Edit(string id)
        {
            ViewBag.RoleList = roleService.List();
            if (string.IsNullOrEmpty(id))
            {
                ViewData["title"] = "新增角色";
                return View();
            }
            else
            {
                ViewData["title"] = "编辑角色";
                var role = roleService.Get(id);

                return View(role);
            }
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="post"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Edit(Role role)
        {
            ViewBag.RoleList = roleService.List();
            var id = roleService.Save(role);
            if (Request.IsAjaxRequest())
            {
                return Content(id);
            }
            else
            {
                return RedirectToAction("Edit", "Admin", new { id = id });
            }
        }

        /// <summary>
        /// 查看角色
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns> 
        public ActionResult View(string id)
        {
            ViewBag.RoleList = roleService.List();
            if (!string.IsNullOrEmpty(id))
            {
                var role = roleService.Get(id);

                return View(role);
            }
            else
                return View();
        }

        public ActionResult Delete(string id)
        {
            roleService.Delete(id);

            return Content("");
        }

        protected override void OnException(ExceptionContext filterContext)
        {
            logger.Error(filterContext.RouteData.Values["action"], filterContext.Exception);

            base.OnException(filterContext);
        }
    }
}